<?php
/********************************************************
** Copyright 2010-2013. Todos los derechos reservados. **
** Desarrollado y Gestionado por:					   **
** Sugey Quintero (Analista)						   **
** José G. De Freitas (Programador)					   **
** Klaudia Uribe (Diseñadora Web)					   **
** Cesar Isturiz (Diseñador Web)					   **
**				S E A  v1.0							   **
*********************************************************/

include("conexion.php");

$cedula=$_POST['cedula'];
$lapso=$_POST['lapsos'];
$cod_mat1=$_POST['cod_mat1'];
$cod_mat2=$_POST['cod_mat2'];
$mat_p1=$_POST['mat_p1'];
$mat_p2=$_POST['mat_p2'];

if($cedula && $lapso && $cod_mat1 && $mat_p1){
	if($lapso == '1er Lapso'){
		$sql = mysql_query("UPDATE alumno_matpend SET nota1='$mat_p1' WHERE cedula='$cedula' AND mat_pend='$cod_mat1' ");
		$sql2 = mysql_query("UPDATE alumno_matpend SET nota1='$mat_p2' WHERE cedula='$cedula' AND mat_pend='$cod_mat2' ");
	}else if($lapso == '2do Lapso'){
		$sql = mysql_query("UPDATE alumno_matpend SET nota2='$mat_p1' WHERE cedula='$cedula' AND mat_pend='$cod_mat1' ");
		$sql2 = mysql_query("UPDATE alumno_matpend SET nota2='$mat_p2' WHERE cedula='$cedula' AND mat_pend='$cod_mat2' ");
	}else if($lapso == '3er Lapso'){
		$buscar = mysql_fetch_array(mysql_query("SELECT nota1, nota2 FROM alumno_matpend WHERE cedula='$cedula' AND mat_pend='$cod_mat1' "));
		$final = ($buscar[0] + $buscar[1] + $mat_p1)/3;
		$buscar2 = mysql_fetch_array(mysql_query("SELECT nota1, nota2 FROM alumno_matpend WHERE cedula='$cedula' AND mat_pend='$cod_mat2' "));
		$final2 = ($buscar2[0] + $buscar2[1] + $mat_p2)/3;
		$sql = mysql_query("UPDATE alumno_matpend SET nota3='$mat_p1', nota_final='$final' WHERE cedula='$cedula' AND mat_pend='$cod_mat1' ");
		$sql2 = mysql_query("UPDATE alumno_matpend SET nota3='$mat_p2', nota_final='$final2' WHERE cedula='$cedula' AND mat_pend='$cod_mat2' ");
	}

echo "<script>alert('Datos guardados exitosamente!');</script>";
	if($lapso == '3er Lapso'){
		if($sql && $sql2){
			$materia = mysql_fetch_array(mysql_query("SELECT nombre_m FROM materia WHERE Cod_m='$cod_mat1' "));
			echo "<script>alert('Nota final de $materia[0]: $final');</script>";
			$materia2 = mysql_fetch_array(mysql_query("SELECT nombre_m FROM materia WHERE Cod_m='$cod_mat2' "));
			echo "<script>alert('Nota final de $materia2[0]: $final2');</script>";
		}else if($sql){
			$materia = mysql_fetch_array(mysql_query("SELECT nombre_m FROM materia WHERE Cod_m='$cod_mat1' "));
			echo "<script>alert('Nota final de $materia[0]: $final');</script>";
		}else if($sql2){
			$materia = mysql_fetch_array(mysql_query("SELECT nombre_m FROM materia WHERE Cod_m='$cod_mat2' "));
			echo "<script>alert('Nota final de $materia[0]: $final2');</script>";
		}
	}
echo "<script>document.location.href='index.php';</script>";
}else {
	echo "<script>alert('Error al guardar datos, vuelva a intentarlo llenando todos los campos');</script>";
	echo "<script>document.location.href='NotaMatPend.php?cedula=$cedula&lapso=$lapso';</script>";
}
?>
